package com.r2coding.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.r2coding.common.util.Query;
import com.r2coding.system.dao.PermissionDao;
import com.r2coding.system.entity.PermissionEntity;
import com.r2coding.system.model.RolePermissionModel;
import com.r2coding.system.service.PermissionService;
import com.r2coding.system.vo.req.PermissionReqVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.r2coding.common.util.PageUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-04-04 11:10:29
 */
@Service("permissionService")
public class PermissionServiceImpl extends ServiceImpl<PermissionDao, PermissionEntity> implements PermissionService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PermissionEntity> page = this.page(
                new Query<PermissionEntity>().getPage(params),
                new QueryWrapper<PermissionEntity>()
        );

        return new PageUtils(page);
    }

    //@Cacheable(value = "permission", key = "#root.methodName")
    @Override
    public List<PermissionEntity> selectList() {
        List<PermissionEntity> permissionEntities = this.baseMapper.selectList(null);
        List<PermissionEntity> list = new ArrayList<>();
        for (PermissionEntity entity : permissionEntities) {
            if (entity.getParentId() == 0) {
                list.add(entity);
            }
        }
        for (PermissionEntity entity : list) {
            childrenMenu(permissionEntities, entity);
        }
        return list;
    }

    private PermissionEntity childrenMenu(List<PermissionEntity> menuList, PermissionEntity menu) {
        List<PermissionEntity> children = new ArrayList<>();
        for (PermissionEntity m : menuList) {
            if (m.getParentId()== menu.getPermissionId()) {
                children.add(childrenMenu(menuList, m));
            }
        }
        menu.setChildren(children);
        return menu;
    }

    // 将角色与权限添加到角色权限关系表中
    @Transactional
    @Override
    public void saveRolePermission(PermissionReqVo vo) {
        Integer roleId = vo.getRoleId();
        // 保存之前 先把该角色拥有的所有权限全部删除
        this.baseMapper.removePermissionByRoleId(roleId);
        List<Integer> ids = new ArrayList<>();
        for (Integer integer : vo.getPermissionIdList()) {
            PermissionEntity entity = this.baseMapper.selectById(integer);
            if (entity.getParentId() > 0) {
                ids.add(integer);
            }
        }

        List<RolePermissionModel> collect = ids.stream().map(item -> {
            RolePermissionModel model = new RolePermissionModel();
            model.setRoleId(roleId);
            model.setPermissionId(item);
            model.setUpdateTime(new Date());
            model.setCreateTime(new Date());
            return model;
        }).collect(Collectors.toList());

        System.out.println("collect="+collect.toString());
        for (RolePermissionModel model : collect) {
            this.baseMapper.saveRolePermission(model);
        }
    }

    @Override
    public List<PermissionEntity> getPermissionsByUserId(Integer userId) {
        List<PermissionEntity> list = this.baseMapper.getPermissionsByUserId(userId);
        return list;
    }
}